package com.hugoo.core.db;

import com.hugoo.core.db.mo.Record;
import com.hugoo.core.db.mo.RecordSet;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Xinton Chan
 */
public class HSF {

    protected static final Log log = LogFactory.getLog(HSF.class);

    public static RecordSet<Record> query(String dbl, String sql, int nCommonPageSize, int nCurrentPage) {
        return new ODC(dbl).query(sql, nCommonPageSize, nCurrentPage);
    }

    public static RecordSet<Record> query(String dbl, String sql) {
        return new ODC(dbl).query(sql, -1, 1);
    }

    /**
     * 打印组件协议
     *
     * @param sql
     * @return
     */
    public static String queryAndRuPrtXml(String dbl, String sql) {
        return new ODC(dbl).queryAndRuPrtXml(sql);
    }

    public static void call(String dbl, Call call) {
        Connection con = Conn.conn(dbl);
        long l = System.currentTimeMillis();
        try {
            CallableStatement cs = con.prepareCall(call.procSql());
            call.proc(cs, log);
            log.info("成功执行存储过程【" + call.procSql() + "】,共耗时" + (System.currentTimeMillis() - l) + "ms。");
        } catch (Exception ex) {
            log.warn("调用存储过程【" + call.procSql() + "】失败,共耗时" + (System.currentTimeMillis() - l) + "ms。", ex);
        } finally {
            Conn.release(con);
        }
    }

    public static RecordSet<Record> query(String dbl, String sql, Map<String, String> m, int cps, int cp) {
        if (m != null) {
            for (String s : m.keySet()) {
                sql = sql.replaceAll(":" + s, "'" + m.get(s) + "'");
            }
        }
        return query(dbl, sql, cps, cp);
    }

    public static RecordSet<Record> query(String dbl, String sql, Map<String, String> m) {
        return query(dbl, sql, m, 150, 1);
    }

    public static int[] excute(String dbl, String... sql) {
        return new ODC(dbl).excute(sql);
    }

    public static int[] excute(String dbl, List<String> sqlList) throws Exception {
        return new ODC(dbl).excute(sqlList);
    }


    public static void saveClob(String dbl, String sql, String column, String value) {
        new ODC(dbl).saveClob(sql, column, value);
    }
}
